<?php

namespace App\Models;

use App\Facades\MoneyPlatformRequestFacade;
use App\Library\Traits\RepayBuckle;
use App\ModelFilters\RepayRecordFilter;
use App\Models\Traits\Repay as RepayTrait;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\RepayRecord as RepayRecordTraits;

/**
 * App\Models\RepayRecord
 *
 * @property int $id
 * @property int $uid 关联用户id
 * @property int $summary_id 关联还款表id
 * @property int $bank_cards_id 关联user_bank_cards银行卡id
 * @property int $period 当前期数,0为全部还清记录
 * @property int $amount 应还款金额/本金,以分为单位
 * @property int $status 还款状态 1:待还／未还,2:还款中,3:还款失败,4:还款成功(以资方还款接口为准)
 * @property int $pay_off 是否全部还清 1:分期还款,2:全部还清
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\RepayRecordCommon $common
 * @property-read \App\Models\RepayRecordMoney $money
 * @property-read \App\Models\RepayRecordMonth $month
 * @property-read \App\Models\RepaySummary $post
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord filter($input = array(), $filter = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereBankCardsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereNormalTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord wherePayOff($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord wherePeriod($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereRepayTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereSummaryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int $failure_status 还款失败细分状态 1:月报接口失败,2:公共接口失败,3:资方接口失败
 * @property string $failure_message 还款失败原因
 * @property string|null $normal_date 本期正常还款日期
 * @property string|null $repay_date 本期真实还款日期(以资方接口成功事件为准)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereFailureMessage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereFailureStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereNormalDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecord whereRepayDate($value)
 * @property-read \App\Models\RepaySummary $relationSummary
 * @property-read \App\Models\UserGlobal $relationUserGlobals
 * @property-read \App\Models\User $users
 */
class RepayRecord extends Model
{
    use Filterable,RepayRecordTraits, RepayTrait, RepayBuckle;

    //record表的status状态 1:待处理,2:处理中,3:失败,4:成功
    const STATUS_ONE = 1;
    const STATUS_TWO = 2;
    const STATUS_THREE = 3;
    const STATUS_FOUR = 4;

    //failure_status 还款失败细分状态 1:月报接口失败,2:公共接口失败,3:资方接口失败,4销账失败,5裸扣失败
    const FAILURE_STATUS_ONE = 1;
    const FAILURE_STATUS_TWO = 2;
    const FAILURE_STATUS_THREE = 3;
    const FAILURE_STATUS_FOUR = 4;
    const FAILURE_STATUS_FIVE = 5;

    //pay_off 是否全部还清 1:分期还款,2:全部还清
    const PAY_OFF_ONE = 1;
    const PAY_OFF_TWO = 2;

    //Repay_type的分类
    const REPAY_TYPE_MANUAL = 1;//手动
    const REPAY_TYPE_AUTO = 2;//自动
    const REPAY_TYPE_WITHHOLDING = 3;//催收代扣
    const REPAY_TYPE_ACCOUNT = 4;//催收销账
    const REPAY_TYPE_REPAYMENT = 5;//催收全部还清
    const REPAY_TYPE_DATA = 6;//异常还款,重新唤起
    const REPAY_TYPE_XIANXIA = 7;//新光大线下提前结清

    const RETURN_TYPE_ONE = 1;//返回前端状态的待还
    const RETURN_TYPE_TWO = 2;//返回前端状态的逾期
    const RETURN_TYPE_THREE = 3;//返回前端状态的已还
    const RETURN_TYPE_FIVE = 5;//返回前端状态的还清

    //第三方(蔚捷)接口还款状态 1:已还款,2:逾期中,3:待还款
    const WJ_STATUS_ONE = 1;
    const WJ_STATUS_TWO = 2;
    const WJ_STATUS_THREE = 3;
    //第三方(蔚捷)接口还款记录状态 1：提前还款，2：正常还款，3：逾期还款）
    const WJ_REPAYMENT_STATUS_ONE = 1;
    const WJ_REPAYMENT_STATUS_TWO = 2;
    const WJ_REPAYMENT_STATUS_THREE = 3;

    //buckle_type字段的枚举值
    const BUCKLE_TYPE_NONE = 0;
    const BUCKLE_TYPE_ONE = 1;

    //裸扣的返回状态
    const ORDER_STATUS_ING = 1;
    const ORDER_STATUS_SUCCESS = 2;
    const ORDER_STATUS_FAIL = 3;

    const COMPENSATE_STATUS_ONE = 1;//未销账
    const COMPENSATE_STATUS_TWO = 2;//销账中
    const COMPENSATE_STATUS_THREE = 3;//销账失败
    const COMPENSATE_STATUS_FOUR = 4;//已销账

    /**
     * 定义默认模型过滤器
     *
     * @return string
     */
    public function modelFilter()
    {
        return $this->provideFilter(RepayRecordFilter::class);
    }

    /**
     * 定义不允许自动填充的字段
     * @var array
     */
    protected $guarded = [
    ];

    /**
     * Description: 关联月报接口表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function month()
    {
        return $this->hasOne('App\Models\RepayRecordMonth', 'record_id', 'id');
    }

    /**
     * Description: 关联公共接口表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function common()
    {
        return $this->hasOne('App\Models\RepayRecordCommon', 'record_id', 'id');
    }

    /**
     * Description: 关联资方接口表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function money()
    {
        return $this->hasOne('App\Models\RepayRecordMoney', 'record_id', 'id');
    }

    /**
     * Description: 反关summary
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationSummary()
    {
        return $this->belongsTo('App\Models\RepaySummary', 'summary_id', 'id');
    }

    /**
     * Description:
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationUserGlobals()
    {
        return $this->belongsTo('App\Models\UserGlobal', 'uid', 'uid');
    }

    /**
     * Description:反关users表
     * Author:lijiafei
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function users(){
        return $this->belongsTo('App\Models\User','uid','id');
    }

    /**
     * 设置分期记录本金
     *
     * @param  string  $value
     * @return void
     */
    public function setAmountAttribute($value)
    {
        $this->attributes['amount'] = (int)bcmul($value, 100);
    }

    /**
     * Description: 获取分期记录本金
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getAmountAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    public function getLoanIdAttribute(){
        if(empty($this->summary_id)){
            $summary_id = self::where('id',$this->id)->value('summary_id');
        }else{
            $summary_id = $this->summary_id;
        }
        return \App\Models\RepaySummary::where('id',$summary_id)->value('loan_id');
    }

    /**
     * 只查询正在销账中的数据
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePayCompensating($query)
    {
        return $query->whereIn('xz_status',[RepayRecord::COMPENSATE_STATUS_ONE,RepayRecord::COMPENSATE_STATUS_TWO])->where(['repay_type' => RepayRecord::REPAY_TYPE_ACCOUNT]);
    }

    /**
     * Description: 反关用户表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationUsers()
    {
        return $this->belongsTo('App\Models\User', 'uid', 'id');
    }

    /**
     * Description: 反关用户身份证实名表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationUserCertOcrs()
    {
        return $this->belongsTo('App\Models\UserCertOcr', 'uid', 'user_id');
    }

    /**
     * Description:判断上期是否还成功
     * Author:lijiafei
     * @param $recordInfo
     * @return bool
     */
    public static function isLastPeriodSuccess($recordInfo){
        $period = $recordInfo->period;
        $summary_id = $recordInfo->summary_id;
        if($period == 1 || $period == 0){
            //第一期和全部结清不进行判断
            return true;
        }
        $res = self::where('summary_id',$summary_id)->where('period','<',$period)->where('status','<>',4)->first();
        if(empty($res)){
            return true;
        }
        return false;
    }
}